package com.loeng.module.infra.controller.admin.file;

import cn.hutool.core.io.IoUtil;
import com.loeng.framework.apilog.core.annotation.ApiAccessLog;
import com.loeng.framework.common.pojo.CommonResult;
import com.loeng.framework.common.pojo.PageParam;
import com.loeng.framework.common.pojo.PageResult;
import com.loeng.framework.common.util.object.BeanUtils;
import com.loeng.framework.excel.util.ExcelUtils;
import com.loeng.module.infra.controller.admin.file.vo.file.FilePageReqVO;
import com.loeng.module.infra.controller.admin.file.vo.file.FileRespVO;
import com.loeng.module.infra.controller.admin.file.vo.file.FileSaveReqVO;
import com.loeng.module.infra.controller.admin.file.vo.file.FileUploadReqVO;
import com.loeng.module.infra.dal.dataobject.file.FileDO;
import com.loeng.module.infra.service.file.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.loeng.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.loeng.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 文件")
@RestController
@RequestMapping("/infra/file")
@Validated
public class FileController {

    @Resource
    private FileService fileService;

    @PostMapping("/upload")
    @Operation(summary = "上传文件", description = "模式一：后端上传文件")
    public CommonResult<String> uploadFile(FileUploadReqVO uploadReqVO) throws Exception{
        MultipartFile file = uploadReqVO.getFile();
        String path = uploadReqVO.getPath();
        return success(fileService.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream())));
    }

    @PostMapping("/create")
    @Operation(summary = "创建文件")
    @PreAuthorize("@ss.hasPermission('infra:file:create')")
    public CommonResult<Long> createFile(@Valid @RequestBody FileSaveReqVO createReqVO) {
        return success(fileService.createFile(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新文件")
    @PreAuthorize("@ss.hasPermission('infra:file:update')")
    public CommonResult<Boolean> updateFile(@Valid @RequestBody FileSaveReqVO updateReqVO) {
        fileService.updateFile(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文件")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('infra:file:delete')")
    public CommonResult<Boolean> deleteFile(@RequestParam("id") Long id) {
        fileService.deleteFile(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文件")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('infra:file:query')")
    public CommonResult<FileRespVO> getFile(@RequestParam("id") Long id) {
        FileDO file = fileService.getFile(id);
        return success(BeanUtils.toBean(file, FileRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得文件分页")
    @PreAuthorize("@ss.hasPermission('infra:file:query')")
    public CommonResult<PageResult<FileRespVO>> getFilePage(@Valid FilePageReqVO pageReqVO) {
        PageResult<FileDO> pageResult = fileService.getFilePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FileRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出文件 Excel")
    @PreAuthorize("@ss.hasPermission('infra:file:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFileExcel(@Valid FilePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FileDO> list = fileService.getFilePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "文件.xls", "数据", FileRespVO.class,
                        BeanUtils.toBean(list, FileRespVO.class));
    }

}